package com.sixth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.sixth.bean.MarketAdmin;
import com.sixth.bean.MarketAdminExample;
import com.sixth.bean.bo.AdminListBO;
import com.sixth.bean.vo.AdminListVO;
import com.sixth.constant.Constant;
import com.sixth.mapper.MarketAdminMapper;
import com.sixth.service.AdminAdminService;
import com.sixth.util.EncryptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class AdminAdminServiceImpl implements AdminAdminService {

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Transactional
    @Override
    public int deleteByPrimaryKey(Integer id) {
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setId(id);
        marketAdmin.setDeleted(true);
        int code = marketAdminMapper.updateByPrimaryKeySelective(marketAdmin);
        if (code == 0) {
            return 404;
        }
        return 200;
    }

//     @Override
//     public AdminListVO query(BasePageInfo basePageInfo) {
//
//         PageHelper.startPage(basePageInfo.getPage(),basePageInfo.getLimit());
//
//         MarketAdminExample example = new MarketAdminExample();
//         example.setDistinct(true);
//
//         List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);
//
//         PageInfo<MarketAdmin> pageInfo = new PageInfo<>(marketAdmins);
//
//         List<AdminOfAdminListVO> list = new ArrayList<>();
//         for (MarketAdmin marketAdmin : marketAdmins) {
//             AdminOfAdminListVO adminOfAdminListVO = new AdminOfAdminListVO();
//             adminOfAdminListVO.setId(marketAdmin.getId());
//             adminOfAdminListVO.setUsername(marketAdmin.getUsername());
//             adminOfAdminListVO.setAvatar(marketAdmin.getAvatar());
// //            adminOfAdminListVO.setRoleIds(marketAdmin.getRoleIds());
//             list.add(adminOfAdminListVO);
//         }
//
//         AdminListVO adminListVO = new AdminListVO();
//
//         long total = pageInfo.getTotal();
//
//         adminListVO.setTotal(pageInfo.getTotal());//查询数据总行数
//         adminListVO.setLimit(pageInfo.getPageSize());//每一页有多少数据
//         adminListVO.setPage(pageInfo.getPageNum());//当前页
//         adminListVO.setPages(pageInfo.getPages());//一共有多少页
//         // page limit total
//         // 把mybatis查询出来的原始结果放进去
//         adminListVO.setList(list);
//
//         return  adminListVO;
//
//     }

    @Transactional
    @Override
    public void modify(MarketAdmin admin) {

        // 对密码进行加密
        String password = EncryptionUtil.getMD5String(admin.getPassword(), admin.getUsername());
        admin.setPassword(password);

        marketAdminMapper.updateByPrimaryKeySelective(admin);//编辑

    }

    @Override
    public MarketAdmin create(MarketAdmin admin) {
        // 给密码加密
        admin.setPassword(EncryptionUtil.getMD5String(admin.getPassword(), admin.getUsername()));
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        int code = marketAdminMapper.insertSelective(admin);
        if (code == 0) {
            return null;
        }

        return admin;
    }

    @Override
    public AdminListVO list(AdminListBO adminListBO, String username) {
        PageHelper.startPage(adminListBO.getPage(), adminListBO.getLimit());

        MarketAdminExample marketAdminExample = new MarketAdminExample();
        marketAdminExample.setOrderByClause(adminListBO.getSort()+" "+adminListBO.getOrder());
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andDeletedEqualTo(Constant.EXIST);
        if (adminListBO.getUsername() != null) {
            criteria.andUsernameEqualTo(adminListBO.getUsername());
        }
        marketAdminExample.setOrderByClause(adminListBO.getSort() + " " + adminListBO.getOrder());

        criteria.andDeletedEqualTo(false);

        if (!StringUtil.isEmpty(username)){
            criteria.andUsernameLike("%" + username + "%");
        }
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExampleBySelf(marketAdminExample);
        PageInfo<MarketAdmin> pageInfo = new PageInfo<>(marketAdmins);

        AdminListVO adminListVO = new AdminListVO();
        adminListVO.setTotal(pageInfo.getTotal());
        adminListVO.setPage(pageInfo.getPageNum());
        adminListVO.setPages(pageInfo.getPages());
        adminListVO.setLimit(pageInfo.getPageSize());

        adminListVO.setList(marketAdmins);

        return adminListVO;

    }

}
